package com.wangp.mywrite.inwork.java_advance.raw_type;

import com.beust.jcommander.internal.Lists;
import java.lang.reflect.Method;
import java.util.List;
import org.springframework.core.ResolvableType;

/***
 * @author wangpeng
 * @since 2022-03-25  11:38
 */
public class ResolvableTypeUse {

    public static void main(String[] args) throws NoSuchMethodException {
        fetchGenerics();

    }

    /**
     * 提取泛型
     */
    private static void fetchGenerics() {
        Class<Template> templateClass = Template.class;
        int loopCount = 0;
        List<String> methodList = Lists.newArrayList();

        for (Method method : templateClass.getMethods()) {
            loopCount++;
            methodList.add(method.getName());
            // 参数数量等于0 跳过
            if (method.getParameterCount() == 0) {
                continue;
            }
            // 获取到方法参数
            ResolvableType methodParameter = ResolvableType.forMethodParameter(method, 0);

            // isAssignableFrom
            if (BaseClass.class.isAssignableFrom(methodParameter.getRawClass())) {
                String typeName = methodParameter.getType().getTypeName();
                System.out.println("参数类型名称：" + typeName);
                for (ResolvableType generic : methodParameter.getGenerics()) {
                    System.out.println("泛型名称：" + generic.getType().getTypeName());
                }
            }
        }
        System.out.println("循环了 " + loopCount + "次，遍历的方法有：" + methodList);
    }
}
